package com.cn.web;

import com.alibaba.fastjson.JSONObject;
import com.cn.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by chenyuncong on 2018/9/7.
 */
@RestController
@RequestMapping("rest")
public class RestTestController {
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("post")
    public String post(){

        String url="http://127.0.0.1:8899/Chuzu/api/v1/Access/gpsupload";
        JSONObject postData = new JSONObject();
        postData.put("gpslength", 1);
        TestBean tb= new TestBean("粤A12345","000000","2018-09-06 10:00:00",1,113.01234,23.56789,100,0,3000,1,60,0,0);
        List<TestBean> list = new ArrayList<>();
        list.add(tb);
        postData.put("gpsitems",list);
        JSONObject json = restTemplate.postForEntity(url, postData, JSONObject.class).getBody();

        System.out.println(json);
        return "";
    }


    @RequestMapping("post2")
    public String post2(){

        String url="http://127.0.0.1:8899/Chuzu/api/v1/Access/meterupload";
        String jsons="{\"plate\":\"粤A54321\",\"cert\":\"999999\",\"meterid\":\"000001\",\"meterno\":\"123456\",\"meterseq\":1,\"meterkvalue\":99,\"emptytime\":\"2018-09-06 10:50:00\",\"workbegintime\":\"2018-09-06 11:00:00\",\"workendtime\":\"2018-09-06 11:30:00\",\"emptygps\":{\"plate\":\"粤A54321\",\"cert\":\"000000\",\"gpstime\":\"2018-09-06 10:50:00\",\"runstatus\":1,\"lon\":113.01234,\"lat\":23.56789,\"speed\":100,\"dir\":0,\"height\":3000,\"eff\":1,\"miles\":60,\"alarm\":0,\"status\":0},\"workbegingps\":{\"plate\":\"粤A54321\",\"cert\":\"000000\",\"gpstime\":\"2018-09-06 11:00:00\",\"runstatus\":1,\"lon\":113.01234,\"lat\":23.56789,\"speed\":100,\"dir\":0,\"height\":3000,\"eff\":1,\"miles\":60,\"alarm\":0,\"status\":0},\"workendgps\":{\"plate\":\"粤A54321\",\"cert\":\"000000\",\"gpstime\":\"2018-09-06 11:30:00\",\"runstatus\":1,\"lon\":113.01234,\"lat\":23.56789,\"speed\":100,\"dir\":0,\"height\":3000,\"eff\":1,\"miles\":60,\"alarm\":0,\"status\":0},\"emptymiles\":2,\"loadmiles\":8,\"metermoney\":32,\"uintprice\":3.5,\"slowtime\":60,\"loadnum\":3,\"teamcode\":\"C1500\",\"evaluate\":2,\"paymode\":\"现金\",\"paytime\":\"2018-09-06 11:31:00\",\"orderid\":\"\"}";
        JSONObject x= JSONObject.parseObject(jsons,JSONObject.class);
        JSONObject json = restTemplate.postForEntity(url, x, JSONObject.class).getBody();
        System.out.println(json);
        return "";
    }


    public static void main(String[] args) {
//        String jsons="{\"plate\":\"粤A54321\",\"cert\":\"999999\",\"meterid\":\"000001\",\"meterno\":\"123456\",\"meterseq\":1,\"meterkvalue\":99,\"emptytime\":\"2018-09-06 10:50:00\",\"workbegintime\":\"2018-09-06 11:00:00\",\"workendtime\":\"2018-09-06 11:30:00\",\"emptygps\":{\"plate\":\"粤A54321\",\"cert\":\"000000\",\"gpstime\":\"2018-09-06 10:50:00\",\"runstatus\":1,\"lon\":113.01234,\"lat\":23.56789,\"speed\":100,\"dir\":0,\"height\":3000,\"eff\":1,\"miles\":60,\"alarm\":0,\"status\":0},\"workbegingps\":{\"plate\":\"粤A54321\",\"cert\":\"000000\",\"gpstime\":\"2018-09-06 11:00:00\",\"runstatus\":1,\"lon\":113.01234,\"lat\":23.56789,\"speed\":100,\"dir\":0,\"height\":3000,\"eff\":1,\"miles\":60,\"alarm\":0,\"status\":0},\"workendgps\":{\"plate\":\"粤A54321\",\"cert\":\"000000\",\"gpstime\":\"2018-09-06 11:30:00\",\"runstatus\":1,\"lon\":113.01234,\"lat\":23.56789,\"speed\":100,\"dir\":0,\"height\":3000,\"eff\":1,\"miles\":60,\"alarm\":0,\"status\":0},\"emptymiles\":2,\"loadmiles\":8,\"metermoney\":32,\"uintprice\":3.5,\"slowtime\":60,\"loadnum\":3,\"teamcode\":\"C1500\",\"evaluate\":2,\"paymode\":\"现金\",\"paytime\":\"2018-09-06 11:31:00\",\"orderid\":\"\"}";
//        JSONObject x= JSONObject.parseObject(jsons,JSONObject.class);
//        System.out.println(x);
    }

    @GetMapping("getFile")
    public String getFile(HttpServletResponse response){
        String path="http://14.29.38.96:8004/3/5?DownType=3&jsession=0ed351b1-c077-4c9c-9ef8-44a514b01818&DevIDNO=866873029738229&FLENGTH=18028755&FOFFSET=0&MTYPE=1&FPATH=/mnt/m_external_sd/YC8300A/video/0-1-20181011000038_A.mp4&SAVENAME=0-1-20181011000038_A.mp4";
        download(path,response);
        return "ok";
    }

    public HttpServletResponse download(String path, HttpServletResponse response) {
        try {
            // path是指欲下载的文件的路径。
            File file = new File(path);
            // 取得文件名。
            String filename = file.getName();
            // 取得文件的后缀名。
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return response;
    }
}
